package com.singletonapps.evory.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;

import com.singletonapps.evory.dto.UserDto;

@Entity
@Table(name = "USERS")
public class User implements Serializable {
	
    private static final long serialVersionUID = -3075369186882103498L;

    @Id
	@SequenceGenerator(name="user_seq", sequenceName="SEQ_USERS_ID", allocationSize=1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
	@Column(name = "ID")
	private Long id;
	
	@Size(max=30)
	@Column(name = "USER_FIRST_NAME", nullable = false)
	private String firstName;
	
	@Size(max=30)
	@Column(name = "USER_LAST_NAME", nullable = false)
	private String lastName;
	
	@Size(max=50)
	@Column(name = "USER_EMAIL", nullable = false)
	private String email;
	
	@Size(max=50)
	@Column(name = "USER_LOGIN_NAME", nullable = false)
	private String userName;
	
	@Size(max=255)
	@Column(name = "USER_PASSWORD", nullable = false)
	private String password;
	
	@Max(1)
	@Column(name = "USER_STATUS")
	private Integer status;
	
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "USERS_ROLE", 
				joinColumns = @JoinColumn(name = "USER_ID"), 
				inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
	private List<Role> role = new ArrayList<Role>();
	
	@Column(name = "DATE_CREATE", nullable = false)
	private Date dateCreated;
	
	@Column(name = "DATE_UPDATE")
	private Date dateUpdated;
	
	@Version
	private int version;
	
	public User() {}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = new Date(dateCreated.getTime());
	}
	public Date getDateUpdated() {
		return dateUpdated;
	}
	public void setDateUpdated(Date dateUpdated) {
		this.dateUpdated = new Date(dateUpdated.getTime());
	}
	public Integer getVersion() {
		return version;
	}
	public void setVersion(Integer version) {
		this.version = version;
	}
	public List<Role> getRole() {
		return role;
	}
	public void setRole(List<Role> role) {
		this.role = role;
	}

	public UserDto toDto(){
	    UserDto dto = new UserDto();
	    dto.setId(this.id);
	    dto.setNombre(this.firstName);
	    dto.setApellido(this.lastName);
	    dto.setEmail(this.email);
	    dto.setUsuario(this.userName);
	    dto.setStatus(this.status);
	    
	    return dto;
	}
	
}
